const express=require('express')
const router=express.Router()
const connection=require('../../util/request')
let data={}
//查询所有文章、根据条件查询所有文章
router.post('/article/articleList',(req,resp,next)=>{
  let Num=parseInt(req.body.pageSize)*(parseInt(req.body.pageNum)-1)
  let sql='select articleId,articleName,u.username as create_user,create_time,update_time,article_remark,article_context   from  article,art_u,`user` u WHERE article.articleId=art_u.article_id AND art_u.uid=u.uid AND article.article_delStatus=0'
//  判断有无条件进行查询
  if(!req.body.queryWhere){
    //判断返回数据多少个
    sql='select articleId,articleName,u.username as create_user,create_time,update_time,article_remark,article_context   from  article,art_u,`user` u WHERE article.articleId=art_u.article_id AND art_u.uid=u.uid AND article.article_delStatus=0'
    connection.query(sql,(err,result,faild)=>{
      if(err) next(err)
      data.total=result.length
      console.log(result)
      //分页，这里不进行条件查询
      sql=sql+` limit ${Num},${req.body.pageSize}`
      connection.query(sql,(err,result,faild)=>{
        if(err) next(err)
        data.result=result
        data.code=200
        resp.status(200).json(data)
      })
    })
    
  }else{
    //条件查询
    //判断返回数据多少个
    sql=sql+` AND articleName like '%${req.body.queryWhere}%'`
    connection.query(sql,(err,result,faild)=>{
      if(err) next(err)
      data.total=result.length
      //条件查询
      //分页
      sql = sql + ` limit ${Num},${req.body.pageSize} `
      connection.query(sql,(err,result,faild)=>{
        if(err) next(err)
        data.result=result
        data.code=200
        resp.status(200).json(data)
      })
    })
    
    
  }
})
// 添加文章
router.put('/article/addArticle',(req,resp,next)=>{
  if(req.body.articleName&&req.body.articleContent){
    let uid=req.user.uid
    let articleId=null
    let sql=`SELECT articleName FROM article WHERE articleName='${req.body.articleName}' `
    connection.query(sql,(err,result,faild)=>{
      if(err) next(err)
      if(result.length!=0){
        data.result=[]
        data.message='文章名称重复'
        data.code=409
        resp.status(200).json(data)
      }else{
        connection
        sql=`insert into article(articleName,create_time,update_time,article_remark,article_context) VALUES('${req.body.articleName}',NOW(),NOW(),'${req.body.articleRemark}','${req.body.articleContent}')`
        connection.query(sql,(err,result,faild)=>{
          if(err) next(err)
          data.result=[]
          data.code=201
          data.message='添加成功'
          articleId=result.insertId
          sql=`INSERT INTO art_u(article_id,uid) VALUES(${articleId},${uid})`
          connection.query(sql,(err,result,faild)=>{
            if(err) next(err)
          })
          resp.status(200).json(data)
        })
      }
    })
  }else{
        data.code=409
        data.message='文章标题或内容为空'
        data.reuslt=[]
        resp.status(200).json(data)
  }
  
})
// 根据id删除文章
router.delete('/article/deleteArticle',(req,resp,next)=>{
  let sql=`UPDATE article SET  article.article_delStatus=1 WHERE article.articleId=${req.body.id}`
  connection.query(sql,(err,result,faild)=>{
    if(err) next(err)
    data.result=[]
    data.message='删除成功'
    data.code=204
    sql=`UPDATE article_returnarticle,returnarticle  SET returnarticle.returnArticle_delStatus=1 WHERE  article_returnarticle.returnArticle_id=returnarticle.returnArticle_id  AND article_id=${req.body.id}`
    connection.query(sql,(err,result,faild)=>{
      if(err) next(err)
      resp.status(200).json(data)
    })
    
  })
})
//根据id查询文章
router.post('/article/getArticleById',(req,resp,next)=>{
  let sql=`SELECT  articleName,article_remark,article_context FROM article WHERE articleId=${req.body.id}`
  connection.query(sql,(err,result,faild)=>{
    if(err) next(err)
    data.result=result
    if(result){
      data.code=200
      resp.status(200).json(data)
    }else{
      data.code=409
      data.message='暂无该内容,请检查传入信息'
      resp.status(200).json(data)
    }
    
  })
})
//修改文章
router.post('/article/updateArticle',(req,resp,next)=>{
  let sql=`UPDATE article SET articleName='${req.body.articleName}',article_remark='${req.body.articleRemark}',article_context='${req.body.articleContent}',update_time=NOW() WHERE articleId=${req.body.articleId} `
  connection.query(sql,(err,result,faild)=>{
    if(err) next(err)
    data.result=[]
    data.message='修改成功'
    data.code=201
    resp.status(200).json(data)
  })
})
//查找所有回复，分为条件查询和非条件查询 以及分页
router.post('/article/returnArticle',(req,resp,next)=>{
  let Num=req.body.pageSize*(req.body.pageNum-1)

  let sql=`SELECT ret.returnArticle_id as returnArticle_id,username,returnArticle_data,returnArticle_name,articleName,art.articleId as articleId FROM returnarticle ret JOIN article_returnarticle artret ON ret.returnArticle_id=artret.returnArticle_id JOIN article art ON artret.article_id=art.articleId JOIN art_u ON art_u.article_id=art.articleId JOIN `+'user'+` u ON art_u.uid=u.uid WHERE returnArticle_delStatus=0 AND  returnArticle_name LIKE '%${req.body.queryWhere}%' `
  let sqlQueryId=` AND art.articleId='${req.body.articleId}'`
  let sqlPage=` LIMIT ${Num},${req.body.pageSize}`
  if(!req.body.articleId){
    connection.query(sql,(err,result,faild)=>{
      if(err) next(err) 
      data.total=result.length
    })
    connection.query(sql+sqlPage,(err,result,faild)=>{
      if(err) next(err) 
      data.result=result
      data.code=200
      resp.status(200).json(data)
    })
  }else{
    connection.query(sql+sqlQueryId,(err,result,faild)=>{
      if(err) next(err) 
      data.total=result.length
    })
    connection.query(sql+sqlQueryId+sqlPage,(err,result,faild)=>{
      if(err) next(err) 
      data.result=result
      data.code=200
      resp.status(200).json(data)
    })
  }
  
})
// 添加文章回复
router.put('/article/addReturnArticle',(req,resp,next)=>{
  if(req.body.returnArticlTitle&&req.body.returnArticlContxt){
    let returnArticleId=0
    let uid=req.user.uid
    let sql=`SELECT * FROM returnarticle WHERE returnArticle_name='${req.body.returnArticlTitle}'`
    connection.query(sql,(err,result,faild)=>{
      console.log("执行了")
      if(result.length>0){
        data.code=409
        data.message='文章回复标题重复'
        data.reuslt=[]
        resp.status(200).json(data)
      }else{
        sql=`INSERT INTO returnarticle(returnArticle_name,returnArticle_data,returnArticle_context) VALUES('${req.body.returnArticlTitle}',NOW(),'${req.body.returnArticlContxt}') `
        connection.query(sql,(err,result,faild)=>{
          if(err) next(err)
          returnArticleId=result.insertId
          sql=`INSERT INTO article_returnarticle(article_id,returnArticle_id,returnArticle_user_id) VALUES(${req.body.articleId},${returnArticleId},${uid})`
          connection.query(sql,(err,result,faild)=>{
          if(err) next(err)
          data.result=[]
          data.message='添加成功'
          data.code=201
          resp.status(200).json(data)
        })
        })
      }
    })
  }else{
        data.code=409
        data.message='文章回复标题或内容为空'
        data.reuslt=[]
        resp.status(200).json(data)
  }
})
//如何查看文章回复？
//1、根据回复id查询
//2、传过去文章id
router.get('/article/checkReturnArticle',(req,resp,next)=>{
  let sql = `SELECT returnArticle_name,returnArticle_context FROM returnarticle WHERE returnArticle_id=${req.query.returnArticleId}`
  connection.query(sql,(err,result,faild)=>{
    if(err) next(err)
    data.result=result
    data.code=200
    resp.status(200).json(data)
  })
})
//根据回复id删除回复
router.delete('/article/deleteReturnArticle',(req,resp,next)=>{
  
  let sql=`UPDATE returnarticle SET  returnArticle_delStatus=1 WHERE returnarticle.returnArticle_id=${req.body.returnArticleId}`
  connection.query(sql,(err,result,faild)=>{
    if(err) next(err)
    data.result=[]
    data.message='删除成功'
    data.code=204
    resp.status(200).json(data)
  })
})
module.exports=router